package com.wfs.controller;

import com.wfs.pojo.Result;
import com.wfs.pojo.User;
import com.wfs.service.impl.UserServiceImpl;
import com.wfs.utils.JwtUtil;
import com.wfs.utils.Md5Util;
import com.wfs.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;


import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@Validated
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    // 注册新用户

    @PostMapping("/user/register")
    public Result<String> register(@Pattern(regexp = "^\\S{5,16}$")String username, @Pattern(regexp = "^\\S{5,16}$")String password) {
        // 根据用户名从数据库查找，若用户名重复，则直接返回
        if (userService.findByUsername(username) != null) {
        return Result.error("用户名已存在");
         }
        // 注册新用户
        userService.save(username, password);
        return Result.success("注册成功");
}
    // 登录
    @PostMapping("/user/login")
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$")String username, @Pattern(regexp = "^\\S{5,16}$")String password) {
        //根据用户名查询user
        User user = userService.findByUsername(username);
        //判断用户是否存在
        if (user == null) {
            return Result.error("用户不存在");
        }
        // 判断密码是否正确
        if (Md5Util.checkPassword(Md5Util.getMD5String(password),user.getPassword())) {
            return Result.error("密码错误");
        }
        // 生成jwt
        Map<String, Object> claims = new HashMap<>();
        claims.put("username", user.getUsername());
        claims.put("id", user.getId());
        String token= JwtUtil.genToken(claims);
        // 返回token
        return Result.success(token);
    }
    // 获取用户详细信息
    @GetMapping("/user/userInfo")
    public Result<User> getInfo(/*@RequestHeader(name="Authorization") String token*/) {
        // 解析token
        /*Map<String, Object> claims = JwtUtil.parseToken(token);*/
        Map<String, Object> claims = ThreadLocalUtil.get();
        // 根据用户名从数据库查找用户
        User user = userService.findByUsername((String) claims.get("username"));
        return Result.success(user);
    }
    // 更新用户基本信息
    @PutMapping("/user/update")
    public Result<String> update(@RequestBody @Validated User user) {
        userService.update(user);
        return Result.success("更新成功");
    }
    // 更新用户头像
    @PatchMapping("/user/updateAvatar")
    public Result<String> updateAvatar(@RequestParam @URL String avatarUrl) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        // 根据用户名从数据库查找用户
        User user = userService.findByUsername((String) claims.get("username"));
        Integer userId = user.getId();
        userService.updateAvatar(avatarUrl,userId);
        return Result.success("更新成功");
    }
    // 更新用户密码
    @PatchMapping("/user/updatePwd")
    public Result<String> updatePassword(@RequestBody Map<String,String> params) {
        String oldPassword = params.get("old_pwd");
        String newPassword = params.get("new_pwd");
        String rePassword = params.get("re_pwd");
        // 根据用户名从数据库查找用户
        Map<String, Object> claims = ThreadLocalUtil.get();
        // 根据用户名从数据库查找用户
        User user = userService.findByUsername((String) claims.get("username"));
        Integer userId = user.getId();
        // 判断旧密码是否正确
        if (!Md5Util.checkPassword(Md5Util.getMD5String(oldPassword),user.getPassword())) {
            return Result.error("旧密码错误");
        }
        // 判断新密码和确认密码是否一致
        if (!newPassword.equals(rePassword)) {
            return Result.error("两次密码不一致");
        }
        // 更新密码
        userService.updatePassword(newPassword,userId);
        return Result.success("更新成功");
    }
}